package com.springboot.demo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.springboot.demo.entity.*;
import com.springboot.demo.exception.BusinessException;
import com.springboot.demo.mapper.MapMapper;
import com.springboot.demo.mapper.SceneMapper;
import com.springboot.demo.service.MapService;
import com.springboot.demo.utils.Constant;
import com.springboot.demo.utils.PageUtils;
import com.springboot.demo.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wubc683
 * @desc
 * @date 2021-11-18 17:07
 */
@Service
public class MapServiceImpl implements MapService {
    @Autowired
    MapMapper mapMapper;
    @Autowired
    SceneMapper sceneMapper;
    @Autowired
    PageUtils pageUtils;


    @Override
    public MapData getMonitorMap(Long uid) {
        MapData mapData = null;
        mapData = mapMapper.getMonitorMapByUid(uid);
        // 第一次获取会先创建一个默认的地图样式
        if (mapData == null) {
            mapData = saveDefaultMonitorMap();
            mapMapper.saveMap(mapData);
            mapMapper.saveUserMonitorMap(uid, mapData.getId());
        }
        return mapData;
    }

    @Override
    public PageResult getMapScenes(Long uid, String search, PageRequest pageRequest) {
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        // 获取所有在运行的场景
        List<Scene> scenesByUidAndSearch = sceneMapper.getScenesByUidAndSearch(uid, search, Constant.SceneStatus.RUNNING);
        return pageUtils.getPageResult(pageRequest, new PageInfo<>(scenesByUidAndSearch));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMonitorMap(Long uid, MapData mapData) {
        // 检查地图id是否属于用户
        Long userIdByMapId = mapMapper.getUserIdByMapId(mapData.getId());
        if (userIdByMapId == null || !userIdByMapId.equals(uid)) {
            throw new BusinessException(ResponseResult.USER_DO_NOT_HAVE_THIS_MAP);
        }
        // 校验聚合类型
        if (!mapData.getDisperseType().equals(Constant.MapDisperseType.POINT_AGGREGATE)
                && !mapData.getDisperseType().equals(Constant.MapDisperseType.POINT_DISPERSE)
                && !mapData.getDisperseType().equals(Constant.MapDisperseType.CITY_AGGREGATE)) {
            throw new BusinessException(ResponseResult.USER_MAP_DO_NOT_HAVE_THIS_AGGREGATE_TYPE);
        }
        // 校验颜色
        if (!mapData.getColor().equals(Constant.MapColor.WHITE)
                && !mapData.getColor().equals(Constant.MapColor.BLUE)
                && !mapData.getColor().equals(Constant.MapColor.ORANGE)
                && !mapData.getColor().equals(Constant.MapColor.GREEN)) {
            throw new BusinessException(ResponseResult.USER_MAP_DO_NOT_HAVE_THIS_COLOR);
        }
        // 校验缩放值
        if (mapData.getScale().compareTo(Constant.MAP_SCALE_MIN_VALUE) < 0
                || mapData.getScale().compareTo(Constant.MAP_SCALE_MAX_VALUE) > 0) {
            throw new BusinessException(ResponseResult.USER_MAP_DO_NOT_HAVE_THIS_SCALE);
        }
        // 校验起始坐标
        if (mapData.getLongitude().compareTo(Constant.MAP_LONGITUDE_MIN_VALUE) < 0
                || mapData.getLongitude().compareTo(Constant.MAP_LONGITUDE_MAX_VALUE) > 0
                || mapData.getLatitude().compareTo(Constant.MAP_LATITUDE_MIN_VALUE) < 0
                || mapData.getLatitude().compareTo(Constant.MAP_LATITUDE_MAX_VALUE) > 0) {
            throw new BusinessException(ResponseResult.MAP_COORDINATE_CROSS_BOUNDS);
        }
        // 修改数据库
        mapMapper.updateMapData(mapData);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMonitorMapPosition(Long uid, MapData mapData) {
        // 检查地图id是否属于用户
        Long userIdByMapId = mapMapper.getUserIdByMapId(mapData.getId());
        if (userIdByMapId == null || !userIdByMapId.equals(uid)) {
            throw new BusinessException(ResponseResult.USER_DO_NOT_HAVE_THIS_MAP);
        }
        // 校验缩放值
        if (mapData.getScale().compareTo(Constant.MAP_SCALE_MIN_VALUE) < 0
                || mapData.getScale().compareTo(Constant.MAP_SCALE_MAX_VALUE) > 0) {
            throw new BusinessException(ResponseResult.USER_MAP_DO_NOT_HAVE_THIS_SCALE);
        }
        // 校验起始坐标
        if (mapData.getLongitude().compareTo(Constant.MAP_LONGITUDE_MIN_VALUE) < 0
                || mapData.getLongitude().compareTo(Constant.MAP_LONGITUDE_MAX_VALUE) > 0
                || mapData.getLatitude().compareTo(Constant.MAP_LATITUDE_MIN_VALUE) < 0
                || mapData.getLatitude().compareTo(Constant.MAP_LATITUDE_MAX_VALUE) > 0) {
            throw new BusinessException(ResponseResult.MAP_COORDINATE_CROSS_BOUNDS);
        }
        // 修改数据库
        mapMapper.updateMapPosition(mapData);
    }

    /**
     * 创建默认监控地图
     *
     * @param
     * @return: void
     */
    private MapData saveDefaultMonitorMap() {
        MapData mapData;// 创建默认监控地图
        mapData = new MapData();
        mapData.setDisperseType(Constant.MapDisperseType.POINT_AGGREGATE);
        mapData.setColor(Constant.MapColor.WHITE);
        mapData.setLongitude(114.044627);
        mapData.setLatitude(22.640208);
        mapData.setScale(12);
        return mapData;
    }
}
